import axios from "axios";
import Store from "@/store";
import { getTimeout } from "@/utils/strong";
import router from "@/router";
// import { getToken } from "@/utils/strong";

const request = axios.create({
  baseURL: "https://slwl-geteway.itheima.net",
  timeout: 5000,
});
const time = 10000;
const tiemGuoqi = () => {
  const nowtime = +new Date();
  const zhucetiame = getTimeout();
  return nowtime - zhucetiame > time * 5000;
};
request.interceptors.request.use((config) => {
  const token = Store.state.user.token;
  // console.log(token);
  if (token) {
    if (tiemGuoqi()) {
      Store.dispatch("user/removeUserInfo");
      router.push("/login");
      return Promise.reject(new Error("token超时了"));
    }
    config.headers.Authorization = `${token}`;
  }
  return config;
});

request.interceptors.response.use(
  (response) => {
    return response;
  },
  (err) => {
    return Promise.reject(err);
  }
);
export default request;
